﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using HYView;

namespace YJKArch.WallModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWallCreateByCurves : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_data = new WallData();
			if (CmdWallCreateByCurves._hWndRevit == null)
			{
				CmdWallCreateByCurves._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			DlgCurvesToWall dlgCurvesToWall = new DlgCurvesToWall(cmdData);
			try
			{
				dlgCurvesToWall.Show(CmdWallCreateByCurves._hWndRevit);
				int wMsg = 7;
				CmdWallCreateByCurves.SendMessage(CmdWallCreateByCurves._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				for (;;)
				{
					List<string> lineStyleName = dlgCurvesToWall.LayerStyleName();
					IList<Reference> list = new List<Reference>();
					if (!this.SelectCurves(lineStyleName, ref list))
					{
						if (!dlgCurvesToWall.SendESC)
						{
							break;
						}
						dlgCurvesToWall.SendESC = false;
						if (dlgCurvesToWall.PickBotton)
						{
							string layerStyle = "";
							if (this.SelectLineStyleName(ref layerStyle))
							{
								dlgCurvesToWall.SetLayerStyle(layerStyle);
							}
						}
					}
					else
					{
						dlgCurvesToWall.GetWallData(ref this.m_data);
						List<Curve> list2 = new List<Curve>();
						foreach (Reference reference in list)
						{
							Element element = this.m_doc.GetElement(reference);
							if (element.GetType() == typeof(ModelLine))
							{
								ModelLine modelLine = element as ModelLine;
								list2.Add(modelLine.GeometryCurve);
							}
							else if (element.GetType() == typeof(ModelArc))
							{
								ModelArc modelArc = element as ModelArc;
								list2.Add(modelArc.GeometryCurve);
							}
							else if (element.GetType() == typeof(DetailLine))
							{
								DetailLine detailLine = element as DetailLine;
								list2.Add(detailLine.GeometryCurve);
							}
							else if (element.GetType() == typeof(DetailArc))
							{
								DetailArc detailArc = element as DetailArc;
								list2.Add(detailArc.GeometryCurve);
							}
						}
						List<Curve> list3 = new List<Curve>();
						foreach (Curve curve in list2)
						{
							List<Curve> collection = WallCreatorByCurve.FormatCurve(curve, list2, this.m_doc, true);
							list3.AddRange(collection);
						}
						WallCreatorByCurve wallCreatorByCurve = new WallCreatorByCurve(this.m_uiApp, this.m_doc);
						Transaction transaction = new Transaction(this.m_doc, "curve to wall");
						transaction.Start();
						List<List<Wall>> list4 = null;
						try
						{
							list4 = wallCreatorByCurve.CreateWallByCurve(list3, this.m_data.OutType, this.m_data.IdOutLevel, this.m_data.OutOffset, this.m_data.OutWallHeight, dlgCurvesToWall.cbxBreak.Checked);
							if (list4.Count == 0)
							{
								MessageBox.Show("所选区域无法生成墙体！", "盈建科提示");
							}
						}
						catch (Exception)
						{
							dlgCurvesToWall.Close();
							transaction.RollBack();
							return Autodesk.Revit.UI.Result.Failed;
						}
						transaction.Commit();
						wallCreatorByCurve.JoinLinkWalls(list4);
					}
				}
				dlgCurvesToWall.Close();
			}
			catch (Exception ex)
			{
				dlgCurvesToWall.Close();
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}

		private bool SelectCurves(List<string> lineStyleName, ref IList<Reference> selRefs)
		{
			bool result;
			try
			{
				selRefs = this.m_uiDoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new ModelCurveFilter(lineStyleName), "请选择生成墙的参考线：");
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool SelectLineStyleName(ref string lineStyleName)
		{
			Autodesk.Revit.DB.Document document = this.m_uiApp.ActiveUIDocument.Document;
			Reference reference = null;
			Selection selection = this.m_uiApp.ActiveUIDocument.Selection;
			try
			{
				reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ModelCurveFilter(), "选择模型线或详图线");
			}
			catch (Exception)
			{
				return false;
			}
			Element element = document.GetElement(reference);
			if (element.GetType() == typeof(ModelLine))
			{
				ModelLine modelLine = element as ModelLine;
				lineStyleName = modelLine.LineStyle.Name;
			}
			else if (element.GetType() == typeof(ModelArc))
			{
				ModelArc modelArc = element as ModelArc;
				lineStyleName = modelArc.LineStyle.Name;
			}
			else if (element.GetType() == typeof(DetailLine))
			{
				DetailLine detailLine = element as DetailLine;
				lineStyleName = detailLine.LineStyle.Name;
			}
			else if (element.GetType() == typeof(DetailArc))
			{
				DetailArc detailArc = element as DetailArc;
				lineStyleName = detailArc.LineStyle.Name;
			}
			return true;
		}

		public static WindowHandle _hWndRevit;

		public WallData m_data;
	}
}
